using UnityEngine;

[ExecuteInEditMode]
public class ControllerPlatformHinge : ControllerGeneric
{
	public GameObject hinge;

	public GameObject platformPivot;

	public GameObject platform;

	public GameObject platformBalama1;

	public GameObject platformBalama2;

	public GameObject platformBase;

	public float width = 1f;

	private float height = 0.5f;

	public float platformLenght = 1f;

	public float platformAngle;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		PlatformHinge platformHinge = platform.AddComponent<PlatformHinge>();
		platformHinge.width = width;
		platformHinge.height = height;
		platformHinge.platformLenght = platformLenght;
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			width = remoteControl.transform.localScale.x;
		}
		if (width < 0.1f)
		{
			width = 0.1f;
		}
		if (platformLenght < 0.1f)
		{
			platformLenght = 0.1f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, height, height);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, height);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
		platform.transform.localScale = new Vector3(width, platformLenght, height * 0.5f);
		platform.transform.localPosition = new Vector3(0f, (height + platformLenght) / 2f, 0f);
		platformPivot.transform.localEulerAngles = new Vector3(platformAngle, 0f, 0f);
		hinge.transform.localScale = new Vector3(height, (width + 0.4f) / 2f, height);
		platformBalama1.transform.localPosition = Vector3.left * (width - 1f) * 0.5f;
		platformBalama2.transform.localPosition = Vector3.right * (width - 1f) * 0.5f;
		platformBase.transform.localScale = new Vector3((width - 0.3f) / 0.7f, 1f, 1f);
	}

	private void SetParenting()
	{
		platform.transform.parent = base.transform.parent;
		platformBase.transform.parent = base.transform.parent;
		hinge.transform.parent = base.transform.parent;
		platformBalama1.transform.parent = platform.transform;
		platformBalama2.transform.parent = platform.transform;
	}

	private void SetNames()
	{
		platform.name = "Platform Hinge";
		hinge.name = "Platform Hinge Base Collider";
		platformBase.name = "Platform Hinge Base";
	}

	private void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, height);
			remoteControl.gameObject.AddComponent<PresetsPlatformHinge>();
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
		ControllerPlatformHinge component = rc.controller.GetComponent<ControllerPlatformHinge>();
		platformLenght = component.platformLenght;
		platformAngle = component.platformAngle;
		PresetsPlatformHinge component2 = remoteControl.gameObject.GetComponent<PresetsPlatformHinge>();
		component2.platformLenght = component.platformLenght;
		component2.platformAngle = component.platformAngle;
	}

	public void SetupUVs()
	{
		Mesh mesh = platform.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		float num = 0.5f;
		float num2 = 0.5f;
		uv[4].x = 0.5f + width * num;
		uv[4].y = 0.5f + platformLenght * num2;
		uv[5].x = 0.5f - width * num;
		uv[5].y = 0.5f - platformLenght * num2;
		uv[6].x = 0.5f - width * num;
		uv[6].y = 0.5f + platformLenght * num2;
		uv[7].x = 0.5f + width * num;
		uv[7].y = 0.5f - platformLenght * num2;
		uv[0].x = 0.5f - width * num;
		uv[0].y = 0.5f + platformLenght * num2;
		uv[1].x = 0.5f + width * num;
		uv[1].y = 0.5f - platformLenght * num2;
		uv[2].x = 0.5f - width * num;
		uv[2].y = 0.5f - platformLenght * num2;
		uv[3].x = 0.5f + width * num;
		uv[3].y = 0.5f + platformLenght * num2;
		mesh.uv = uv;
	}
}
